package attori.sensori;

import gui.houseeditor.Stanza;
import sde.actor.Message;
import sde.actor.Timer;
import util.Casella;
import attori.controller.ControllerTemperaturaIF;

public class SensoreTemperatura extends Sensore {
	private Stanza stanza;

	private static class Timeout extends Message {
	}

	public static class Accendi extends Message {
	}

	public static class Spegni extends Message {
	}

	private ControllerTemperaturaIF controller;
	private Timer timer = new Timer();
	private Timeout timeout = new Timeout();
	private long periodo;

	public SensoreTemperatura(Stanza stanza,
			ControllerTemperaturaIF controller, long periodo) {
		super();
		this.stanza = stanza;
		this.controller = controller;
		this.periodo = periodo;
		become(OPERATIVE);
		timer.set(timeout, this, periodo);
	}

	@Override
	public void sample() {
		controller.send(new ControllerTemperaturaIF.Temperatura(stanza
				.getTemperatura()));
	}

	@Override
	protected void handler(Message m) {
		switch (currentStatus()) {
		case OPERATIVE:
			if (m == timeout) {
				timer.set(m, this, periodo);
				sample();
			} else {
				if (m instanceof Spegni) {
					become(IDLE);
				}
			}
			break;
		case IDLE:
			if (m instanceof Accendi) {
				become(OPERATIVE);
				send(timeout);
			}
			break;
		}

	}

	@Override
	public Casella[][] getArea() {
		throw new UnsupportedOperationException();
	}

}
